package com.heima.admin.aop;


import com.heima.admin.service.AdHandleLogService;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.admin.pojos.AdHandleLog;
import com.heima.model.admin.pojos.AdUser;
import com.heima.utils.thread.WmThreadLocalUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Date;

/**
 * 监控
 */
@Component
@Aspect
public class LogAdvice {

    @Autowired
    private AdHandleLogService adHandleLogService;
    @Autowired
    private IWemediaClient wemediaClient;


    //添加
    @Pointcut("execution(* com.heima.admin.controller.v1.*.save*(..))")
    private void insert(){}

    //删除
    @Pointcut("execution(* com.heima.admin.controller.v1.*.del*(..))")
    private void delete(){}

    //修改
    @Pointcut("execution(* com.heima.admin.controller.v1.*.update*(..))")
    private void update(){}

    //添加方法
    @After("insert()")
    public void insert(JoinPoint joinPoint){
        saveLog(joinPoint,AdHandleLog.Status.INSERT.getCode());
    }

    //删除方法
    @Before("delete()")
    public void delete(JoinPoint joinPoint){
        AdHandleLog log = new AdHandleLog();
        AdUser adUser = WmThreadLocalUtil.getAdUser();
        log.setUserId(adUser.getId());
        if (joinPoint == null) {
            log.setAfter("记录操作日志失败");
            adHandleLogService.save(log);
        }
        Signature signature = joinPoint.getSignature();
        String methodName = signature.getName();

        //获取删除数据
        Object[] args = joinPoint.getArgs();
        String after = null;
        Integer id = (Integer) args[0];
        if(methodName.contains("Channel")  ){
            after = wemediaClient.findChannelOne(id);
        }else if (methodName.contains("Sensitive")){
            after = wemediaClient.findSensitiveOne(id);
        }else {
            after = Arrays.toString(args);
        }


        log.setType(AdHandleLog.Status.DELETE.getCode());
        log.setModule(checkMoudle(methodName));
        log.setHandleTime(new Date());
        log.setAfter(after);
        adHandleLogService.save(log);
    }

    //修改方法
    @After("update()")
    public void update(JoinPoint joinPoint){
        saveLog(joinPoint,AdHandleLog.Status.UPDATE.getCode());
    }

    //保存日志
    private void  saveLog(JoinPoint joinPoint,Short type){
        AdHandleLog log = new AdHandleLog();
        AdUser adUser = WmThreadLocalUtil.getAdUser();
        log.setUserId(adUser.getId());
        if (joinPoint == null) {
            log.setAfter("记录操作日志失败");
            adHandleLogService.save(log);
        }
        Signature signature = joinPoint.getSignature();
        String methodName = signature.getName();
        Object[] args = joinPoint.getArgs();
        String after = Arrays.toString(args);
        log.setType(type);
        log.setModule(checkMoudle(methodName));
        log.setHandleTime(new Date());
        log.setAfter(after);
        adHandleLogService.save(log);
    }

    //模块信息获取
    private String checkMoudle(String methodName) {
        if(methodName == null){
            return "操作模块获取失败";
        }
        String moudle = "操作模块获取失败";
        if(methodName.contains("AuthNews") || methodName.contains("Channel") || methodName.contains("Sensitive")){
            moudle = methodName + "-->wemedia模块";
        }else if (methodName.contains("AuthUser")){
            moudle = methodName + "-->user模块";
        }
        return moudle;
    }
}
